package com.yzh.openai.trigger.mq.consumoer;

import com.yzh.openai.domain.order.service.IOrderService;
import com.yzh.openai.trigger.mq.producer.RedisPublisher;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Slf4j
@Component
public class RedisSubscriber{
    @Resource
    private IOrderService orderService;
    @Resource
    private RedissonClient redissonClient;

    @PostConstruct
    public void orderListener() {
        RTopic topic = redissonClient.getTopic(RedisPublisher.CHANNEL);
        topic.addListener(String.class, (channel, orderId) -> {
            try {
                log.info("订单支付成功，发货执行开始... 订单：{}", orderId);
                orderService.deliverGoods(orderId);
                log.info("发货完毕！... 订单：{}", orderId);
            } catch (Exception e) {
                log.error("订单支付成功，发货执行失败... 订单：{}", orderId, e);
            }
        });
    }
    
}
